package com.it.data_linkedList.both_way;

import com.it.data_tree.util.ColorPrintln;

import java.util.Iterator;

/**
 * @Author: Coke
 * @DateTime: 2023/05/14/18:33
 * @注释: 双向链表实现
 **/
public class BothLinkList implements Iterable<Integer> {
    // 头哨兵
    BothNode head;
    // 尾哨兵
    BothNode last;
    
    {
        head = new BothNode(null, 666, null);
        last = new BothNode(null, 667, null);
        head.next = last;
        last.prev = head;
    }
    
    /**
     * @param index:
     * @return BothNode
     * @作者: Coke
     * @注释: 根据索引查找节点
     * @DateTime: 2023/5/14 19:03
     */
    public BothNode findNode (int index) {
        int i = - 1;
        for (BothNode p = head; p != last; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }
    
    /**
     * @param index:
     * @param value:
     * @return void
     * @作者: Coke
     * @注释: 指定索引添加元素
     * @DateTime: 2023/5/14 21:27
     */
    public void add (int index, int value) {
        BothNode prev = findNode(index - 1);
        if (prev == null) {
            throw new IllegalArgumentException("无效参数: [" + index + "]");
        }
        BothNode next = prev.next;
        BothNode newNode = new BothNode(prev, value, next);
        prev.next = newNode;
        next.prev = newNode;
    }
    
    /**
     * @param value:
     * @return void
     * @作者: Coke
     * @注释: 头插
     * @DateTime: 2023/5/14 21:29
     */
    public void add (int value) {
        add(0, value);
    }
    
    /**
     * @param value:
     * @return void
     * @作者: Coke
     * @注释: 向末尾处插入值
     * @DateTime: 2023/5/15 20:28
     */
    public void addLast (int value) {
        // 原本的末尾节点
        BothNode lastNode = last.prev;
        // 新节点
        BothNode newNode = new BothNode(lastNode, value, last);
        lastNode.next = newNode;
        last.prev = newNode;
    }
    
    /**
     * @return void
     * @作者: Coke
     * @注释: 遍历
     * @DateTime: 2023/5/14 21:30
     */
    public void print () {
        StringBuilder str = new StringBuilder();
        BothNode node = head.next;
        while (node != null && node.next != last) {
            if (node.prev == head) {
                str.append(node.value);
            } else {
                str.append(", ").append(node.value);
            }
            node = node.next;
        }
        ColorPrintln.greenPrintln(str);
    }
    
    /**
     * @param index:
     * @return BothNode
     * @作者: Coke
     * @注释: 根据索引删除
     * @DateTime: 2023/5/14 21:43
     */
    public BothNode remove (int index) {
        // 删除节点的上一个节点
        BothNode prev = findNode(index - 1);
        if (prev == null) {
            throw new IllegalArgumentException("无效参数: [" + index + "]");
        }
        // 删除节点
        BothNode removed = prev.next;
        if (removed == last) {
            throw new IllegalArgumentException("无效参数: [" + index + "]");
        }
        // 删除节点的下一个节点
        BothNode next = removed.next;
        prev.next = next;
        next.prev = prev;
        return removed;
    }
    
    /**
     * @return void
     * @作者: Coke
     * @注释: 删除最后一个节点
     * @DateTime: 2023/5/15 20:32
     */
    public void removeLast () {
        // 要删除的节点
        BothNode removed = last.prev;
        if (removed == head) {
            throw new IllegalArgumentException("无效参数: [" + 0 + "]");
        }
        BothNode prev = removed.prev;
        prev.next = last;
        last.prev = prev;
    }
    
    /**
     * @return Iterator<Integer>
     * @作者: Coke
     * @注释: 遍历
     * @DateTime: 2023/5/15 21:59
     */
    @Override
    public Iterator<Integer> iterator () {
        return new Iterator<Integer>() {
            BothNode node = head.next;
            
            @Override
            public boolean hasNext () {
                return node != last;
            }
            
            @Override
            public Integer next () {
                int value = node.value;
                node = node.next;
                return value;
            }
        };
    }
}
